package com.hellogood.yaohuo.client.setting;

import java.util.List;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.hellogood.yaohuo.client.common.CommonMessages;
import com.hellogood.yaohuo.shared.services.GroupService;
import com.hellogood.yaohuo.shared.services.GroupUserService;
import com.hellogood.yaohuo.shared.vo.GroupVo;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.TreeModelType;
import com.smartgwt.client.util.BooleanCallback;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.grid.events.SelectionChangedHandler;
import com.smartgwt.client.widgets.grid.events.SelectionEvent;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.tree.Tree;
import com.smartgwt.client.widgets.tree.TreeGrid;
import com.smartgwt.client.widgets.tree.TreeGridField;
import com.smartgwt.client.widgets.tree.TreeNode;

public class GroupController {
	private TreeNode[] treeNodes = null;

	private HLayout layout = null;
	private TreeGrid treeGrid = null;
	private Button addBtn = null;
	private Button delBtn = null;
	private Button setBtn = null;

	public GroupController() {
		initData();
		initView();
	}

	public void initData() {
		GroupUserService.Util.getInstance().getAllGroupUserList(new GroupUserCallback());
	}

	private void initView() {
		TreeGridField nodeIdField = new TreeGridField("nodeId");
		nodeIdField.setHidden(true);
		TreeGridField nameField = new TreeGridField("name", "用户组");
		treeGrid = new TreeGrid();
		treeGrid.setFields(nodeIdField, nameField);
		treeGrid.setWidth("100%");
		treeGrid.setHeight("100%");
		treeGrid.addSelectionChangedHandler(new TreeGridSelectionHandler());

		addBtn = new Button("增加用户组");
		addBtn.setAlign(Alignment.CENTER);
		addBtn.setSize("100", "50");
		addBtn.addClickHandler(new AddBtnClickHandler());
		delBtn = new Button("删除用户组");
		delBtn.setSize("100", "50");
		delBtn.setDisabled(true);
		setBtn = new Button("设置用户组");
		setBtn.addClickHandler(new SettingBtnClickHandler());
		setBtn.setDisabled(true);
		setBtn.setSize("100", "50");

		VLayout vLayout = new VLayout();
		vLayout.setSize("150", "500");
		vLayout.setMembers(addBtn, delBtn, setBtn);
		vLayout.setMembersMargin(20);

		layout = new HLayout();
		layout.setMembers(treeGrid, vLayout);
	}

	public Canvas getCanvas() {
		return layout;
	}

	private class GroupUserCallback implements AsyncCallback<List<GroupVo>> {

		@Override
		public void onFailure(Throwable caught) {
			SC.say(caught.getMessage());
		}

		@Override
		public void onSuccess(List<GroupVo> result) {
			if (result != null) {
				treeNodes = new TreeNode[result.size()];
				for (int i = 0; i < result.size(); i++) {
					GroupVo vo = result.get(i);
					TreeNode r = new TreeNode();
					r.setAttribute("nodeId", vo.getNodeId());
					r.setAttribute("code", vo.getCode());
					r.setAttribute("name", vo.getCode() + "-" + vo.getName());
					r.setAttribute("isOpen", vo.isOpen());
					r.setAttribute("parentId", vo.getParentCode());
					if (vo.getParentCode().equals("0")) {
						r.setAttribute("isFolder", true);
					} else {
						r.setAttribute("isFolder", false);
					}
					treeNodes[i] = r;
				}

				Tree tree = new Tree();
				tree.setModelType(TreeModelType.PARENT);
				tree.setNameProperty("name");
				tree.setRootValue("0");
				tree.setIsFolderProperty("isFolder");
				tree.setIdField("nodeId");
				tree.setParentIdField("parentId");
				tree.setOpenProperty("isOpen");
				tree.setData(treeNodes);

				treeGrid.setData(tree);
			}
		}
	}

	private class TreeGridSelectionHandler implements SelectionChangedHandler {

		@Override
		public void onSelectionChanged(SelectionEvent event) {
			ListGridRecord record = treeGrid.getSelectedRecord();
			if (record != null) {
				if (!record.getAttribute("nodeId").equals("0")) {
					if (!record.getAttribute("nodeId").contains("-")) {
						delBtn.setDisabled(false);
						setBtn.setDisabled(false);
						delBtn.setTitle("删除用户组");
						delBtn.addClickHandler(new GroupController.DeleteQuyuClickHandler());
					} else {
						delBtn.setDisabled(true);
						setBtn.setDisabled(true);
					}
				} else {
					delBtn.setDisabled(true);
					setBtn.setDisabled(true);
				}
			} else {
				delBtn.setDisabled(true);
				setBtn.setDisabled(true);
			}
		}
	}

	private class DeleteQuyuClickHandler implements ClickHandler {

		@Override
		public void onClick(ClickEvent event) {
			SC.confirm(CommonMessages.ConfirmDelete, new BooleanCallback() {

				@Override
				public void execute(Boolean value) {
					if (value != null && value) {
						ListGridRecord record = treeGrid.getSelectedRecord();
						if (record != null) {
							String code = record.getAttribute("code");
							GroupService.Util.getInstance().deleteGroup(code, new AsyncCallback<String>() {

								@Override
								public void onFailure(Throwable caught) {
									SC.say(caught.getMessage());
								}

								@Override
								public void onSuccess(String result) {
									if (result != null) {
										if (!result.equals("")) {
											SC.say(result);
										} else {
											SC.say(CommonMessages.DeleteSuccess);
										}
									}
								}
							});
						}
					}
				}
			});
		}
	}

	private class AddBtnClickHandler implements ClickHandler {

		@Override
		public void onClick(ClickEvent event) {
			GroupModifyWindow window = new GroupModifyWindow(GroupController.this);
			window.setSize("300", "200");
			window.centerInPage();
			window.show();
		}
	}

	private class SettingBtnClickHandler implements ClickHandler {

		@Override
		public void onClick(ClickEvent event) {
			ListGridRecord record = treeGrid.getSelectedRecord();
			if (record != null) {
				String groupCode = record.getAttribute("nodeId");
				if (!groupCode.contains("-")) {
					groupCode = groupCode.replace("-", "");
				}
				GroupSettingWindow window = new GroupSettingWindow(groupCode, record.getAttribute("name"));
				window.setSize("800", "450");
				window.centerInPage();
				window.setTitle("用户组设置");
				window.show();
			}
		}

	}
}
